package com.xueyi.system.BaseDestroyRecords.controller;

import com.xueyi.system.BaseDestroyRecords.domain.query.BaseDestroyRecordsQuery;
import com.xueyi.system.BaseDestroyRecords.domain.dto.BaseDestroyRecordsDto;
import com.xueyi.system.BaseDestroyRecords.service.IBaseDestroyRecordsService;
import com.xueyi.common.web.entity.controller.BaseController;
import com.xueyi.system.material.domain.dto.MaterialDto;
import com.xueyi.system.material.service.IMaterialService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.xueyi.common.security.annotation.Logical;
import com.xueyi.common.security.annotation.RequiresPermissions;
import com.xueyi.common.core.web.validate.V_A;
import com.xueyi.common.core.web.validate.V_E;
import com.xueyi.common.core.web.result.AjaxResult;
import com.xueyi.common.log.annotation.Log;
import com.xueyi.common.log.enums.BusinessType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletResponse;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.List;

/**
 * 销毁记录管理 业务处理
 *
 * @author xueyi
 */
@RestController
@RequestMapping("/destroy")
public class BaseDestroyRecordsController extends BaseController<BaseDestroyRecordsQuery, BaseDestroyRecordsDto, IBaseDestroyRecordsService> {

    @Autowired
    private IMaterialService materialService;

    /** 定义节点名称 */
    @Override
    protected String getNodeName() {
        return "销毁记录" ;
    }

    /**
     * 查询销毁记录列表
     */
    @Override
    @GetMapping("/list")
    @RequiresPermissions(Auth.BASE_DESTROY_RECORDS_LIST)
    public AjaxResult list(BaseDestroyRecordsQuery baseDestroyRecords) {
        return super.list(baseDestroyRecords);
    }

    /**
     * 查询销毁记录详细
     */
    @Override
    @GetMapping(value = "/{id}")
    @RequiresPermissions(Auth.BASE_DESTROY_RECORDS_SINGLE)
    public AjaxResult getInfoExtra(@PathVariable Serializable id) {
        return super.getInfoExtra(id);
    }

    /**
     * 销毁记录导出
     */
    @Override
    @PostMapping("/export")
    @RequiresPermissions(Auth.BASE_DESTROY_RECORDS_EXPORT)
    @Log(title = "销毁记录管理", businessType = BusinessType.EXPORT)
    public void export(HttpServletResponse response, BaseDestroyRecordsQuery baseDestroyRecords) {
        super.export(response, baseDestroyRecords);
    }

    /**
     * 销毁记录新增
     */
    @Override
    @PostMapping
    @RequiresPermissions(Auth.BASE_DESTROY_RECORDS_ADD)
    @Log(title = "销毁记录管理", businessType = BusinessType.INSERT)
    public AjaxResult add(@Validated({V_A.class}) @RequestBody BaseDestroyRecordsDto baseDestroyRecords) {

        MaterialDto materialDto = materialService.selectById(baseDestroyRecords.getRuinId());
        if(baseDestroyRecords.getRuinNum().compareTo(materialDto.getQuantity()) == 1){
            return AjaxResult.error("销毁数量不能大于库存，请确认");
        }
        BigDecimal subtract = materialDto.getQuantity().subtract(baseDestroyRecords.getRuinNum());
        materialDto.setQuantity(subtract);
        materialService.update(materialDto);
        return super.add(baseDestroyRecords);
    }

    /**
     * 销毁记录修改
     */
    @Override
    @PutMapping
    @RequiresPermissions(Auth.BASE_DESTROY_RECORDS_EDIT)
    @Log(title = "销毁记录管理", businessType = BusinessType.UPDATE)
    public AjaxResult edit(@Validated({V_E.class}) @RequestBody BaseDestroyRecordsDto baseDestroyRecords) {
        return super.edit(baseDestroyRecords);
    }

    /**
     * 销毁记录修改状态
     */
    @Override
    @PutMapping("/status")
    @RequiresPermissions(value = {Auth.BASE_DESTROY_RECORDS_EDIT, Auth.BASE_DESTROY_RECORDS_ES}, logical = Logical.OR)
    @Log(title = "销毁记录管理", businessType = BusinessType.UPDATE_STATUS)
    public AjaxResult editStatus(@RequestBody BaseDestroyRecordsDto baseDestroyRecords) {
        return super.editStatus(baseDestroyRecords);
    }

    /**
     * 销毁记录批量删除
     */
    @Override
    @DeleteMapping("/batch/{idList}")
    @RequiresPermissions(Auth.BASE_DESTROY_RECORDS_DEL)
    @Log(title = "销毁记录管理", businessType = BusinessType.DELETE)
    public AjaxResult batchRemove(@PathVariable List<Long> idList) {
        return super.batchRemove(idList);
    }

    /**
     * 获取销毁记录选择框列表
     */
    @Override
    @GetMapping("/option")
    public AjaxResult option() {
        return super.option();
    }

    interface Auth {
        /** 系统 - 销毁记录管理 - 列表 */
        String BASE_DESTROY_RECORDS_LIST = "system:destroy:list" ;
        /** 系统 - 销毁记录管理 - 详情 */
        String BASE_DESTROY_RECORDS_SINGLE = "system:destroy:single" ;
        /** 系统 - 销毁记录管理 - 新增 */
        String BASE_DESTROY_RECORDS_ADD = "system:destroy:add" ;
        /** 系统 - 销毁记录管理 - 修改 */
        String BASE_DESTROY_RECORDS_EDIT = "system:destroy:edit" ;
        /** 系统 - 销毁记录管理 - 修改状态 */
        String BASE_DESTROY_RECORDS_ES = "system:destroy:es" ;
        /** 系统 - 销毁记录管理 - 删除 */
        String BASE_DESTROY_RECORDS_DEL = "system:destroy:delete" ;
        /** 系统 - 销毁记录管理 - 导出 */
        String BASE_DESTROY_RECORDS_EXPORT = "system:destroy:export" ;
    }
}
